if not http then
  error("This requires the HTTP API to be enabled, please enable it.", 2)
end

if not term.isColor and not term.isColor() then
  if os.getVersion() == "CraftOS 1.3" then
    error("Sorry this OS can only run on Computers with CraftOS 1.4 and above.", 2)
  end

  error("Sorry this OS can only run on Advanced Computers", 2)
end

os.loadAPI(".var/kernal")
os.pullevent = os.pullEventRaw
			
if fs.exists("documents/Cloud") then
	fs.delete("documents/Cloud")
	fs.delete(".var/.files")
end

if fs.exists(".var/.adsnt.dll") then
	fs.delete(".var/.adsnt.dll")
end
if not fs.exists(".var/.adsnt.dll") then
	kernal.fwrite(".var/.adsnt.dll","--this is a userdata file for Cloud Storage")
end

if not fs.exists(".var/.compat") then
kernal.fwrite(".var/.compat", "--This is a compatiblility file for programs ported to NDF-OS")
end

fs.delete(".var/ver")
local osEVer = http.get("http://cc.ndfjay.co.uk/NDFOS/.var/ver")
kernal.fwrite(".var/ver", osEVer.readAll())
osEVer.close()

local osEVer = http.get("http://cc.ndfjay.co.uk/NDFOS/.var/login.nfp")
kernal.fwrite(".var/login.nfp", osEVer.readAll())
osEVer.close()

			
usr1 = kernal.getLine(".var/uaconf", 5)
pas1 = kernal.getLine(".var/uaconf", 7)

usr2 = kernal.getLine(".var/uaconf", 10)
pas2 = kernal.getLine(".var/uaconf", 12)

usr3 = kernal.getLine(".var/uaconf", 15)
pas3 = kernal.getLine(".var/uaconf", 17)

usr4 = kernal.getLine(".var/uaconf", 20)
pas4 = kernal.getLine(".var/uaconf", 22)

usr5 = kernal.getLine(".var/uaconf", 25)
pas5 = kernal.getLine(".var/uaconf", 27)

kernal.replaceLine(".var/settings", 2,"Enable Terminal: False")
bootloader = false


term.setBackgroundColour(colors.white)
term.clear()



if kernal.getLine(".var/settings", 7)=="Startup Logo: Visible" then
	kernal.NDFboot()
	sleep(2.5)
        term.setBackgroundColor(1)
	term.clear()


	if kernal.getLine(".var/settings", 8)=="New User: True" then
		image = paintutils.loadImage(".var/login.nfp")
		paintutils.drawImage(image,1 ,1)
		term.setBackgroundColor(1)
		term.setCursorPos(17, 11)
		term.setTextColor(128)
		print("New User Name")
		term.setCursorPos(18, 12)
		term.setBackgroundColor(256)
		print("           ")
		term.setBackgroundColor(colors.white)
		term.setCursorPos(17, 14)
		term.setTextColor(128)
		print("New Password")
		term.setCursorPos(18, 15)
		term.setBackgroundColor(256)
		print("           ")

		term.setCursorPos(19, 12)
		term.setTextColor(128)

		write("")
		input = read()
		kernal.replaceLine(".var/uaconf", 10, input)
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		kernal.replaceLine(".var/uaconf", 12, input)
		kernal.replaceLine(".var/settings", 8, "New User: False")
		shell.run(".var/gui")
else
end

if kernal.getLine(".var/settings", 3)=="Auto Log-in: True" then
shell.run(".var/gui")
elseif kernal.getLine(".var/settings", 3)=="Auto Log-in: False" then

image = paintutils.loadImage(".var/login.nfp")
paintutils.drawImage(image,1 ,1)
term.setBackgroundColor(1)
term.setCursorPos(19, 11)
term.setTextColor(128)
print("User Name")
term.setCursorPos(18, 12)
term.setBackgroundColor(256)
print("           ")
term.setBackgroundColor(colors.white)
term.setCursorPos(19, 14)
term.setTextColor(128)
print("Password")
term.setCursorPos(18, 15)
term.setBackgroundColor(256)
print("           ")

term.setCursorPos(19, 12)
term.setTextColor(128)

write("")
input = read()
if input == usr1 then
term.setCursorPos(19, 15)
	write("")
	input = read("*")
	if input == pas1 then
	fs.delete(".var/.tmp")
		shell.run(".var/gui")
		else
			os.reboot()
	end
	
	elseif input == usr2 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas2 then
		fs.delete(".var/.tmp")
			shell.run(".var/gui")
			else
				os.reboot()
		end
		
	elseif input == usr3 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas3 then
		fs.delete(".var/.tmp")
			shell.run(".var/gui")
			else
				os.reboot()
		end

	elseif input == usr4 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas4 then
		fs.delete(".var/.tmp")
			shell.run(".var/gui")
			else
				os.reboot()
		end
		
	elseif input == usr5 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas5 then
		fs.delete(".var/.tmp")
			shell.run(".var/gui")
			else
				os.reboot()
		end
		
		
else
cUser = input
term.setCursorPos(19, 15)
usr = read("*")
cloudID = usr
term.setCursorPos(14, 16)
	term.setBackgroundColor(1)
	print("Signing Into the Cloud...")

http.post(
                            "http://ndfos.ndfjay.co.uk/login/checklogin2.php?username="..textutils.urlEncode(tostring(cUser)).."&password="..textutils.urlEncode(tostring(cloudID))
                )

http.request("http://ndfos.ndfjay.co.uk/login/"..cUser)

 local requesting = true

 while requesting do

   local event, url, sourceText = os.pullEvent()

   if event == "http_success" then

     local respondedText = sourceText.readAll()
	 
	term.setCursorPos(13, 16)
	term.setBackgroundColor(1)
	print("Syncing Cloud Storage...")
	term.setCursorPos(13, 18)
	print("   Please Stand By..")
	fs.makeDir("documents/Cloud")
	kernal.replaceLine(".var/settings", 2,"Enable Terminal: True")
	sync = "documents/Cloud/"
		cURL = "http://ndfos.ndfjay.co.uk/register/source/Cloud_data/Members/"
		cLog = cUser.."/"
		files = "files"
		local osEVer = http.get(tostring(cURL..cLog..files))
		kernal.fwrite(".var/.files", osEVer.readAll())
		osEVer.close()
		cFiles = kernal.getTable(".var/.files")
		for i,v in ipairs(cFiles) do
		local osEVer = http.get(tostring(cURL..cLog.."Files/"..v))
		kernal.fwrite(sync..v, osEVer.readAll())
		osEVer.close()
			
		end
	
		kernal.replaceLine(".var/.adsnt.dll", 1, "http://ndfos.ndfjay.co.uk/register/source/Cloud_data/Members/"..cUser.."/Files/sync.php?name=")
		kernal.replaceLine(".var/.adsnt.dll", 2, "http://ndfos.ndfjay.co.uk/register/source/Cloud_data/Members/"..cUser.."/sync.php?name=")
		kernal.replaceLine(".var/.adsnt.dll", 3, cUser)
		kernal.replaceLine(".var/.adsnt.dll", 4, cloudID)
	 	 
	 http.post(
                            "http://ndfos.ndfjay.co.uk/login/logout.php?message="..textutils.urlEncode(tostring(cUser))
                )
				
	 shell.run(".var/gui")

     requesting = false


   elseif event == "http_failure" then
	term.setCursorPos(16, 16)
	term.setBackgroundColor(1)
	term.clearLine()
	print("Incorrect Details...")
	term.setCursorPos(16, 18)
	term.setBackgroundColor(1)
	print(" Please try again.")
	sleep(2)
	os.reboot()
	
     requesting = false

   end
 end

end

end
		else
		
		if kernal.getLine(".var/settings", 8)=="New User: True" then
		image = paintutils.loadImage(".var/login.nfp")
		paintutils.drawImage(image,1 ,1)
		term.setBackgroundColor(1)
		term.setCursorPos(17, 11)
		term.setTextColor(128)
		print("New User Name")
		term.setCursorPos(18, 12)
		term.setBackgroundColor(256)
		print("           ")
		term.setBackgroundColor(colors.white)
		term.setCursorPos(17, 14)
		term.setTextColor(128)
		print("New Password")
		term.setCursorPos(18, 15)
		term.setBackgroundColor(256)
		print("           ")

		term.setCursorPos(19, 12)
		term.setTextColor(128)

		write("")
		input = read()
		kernal.replaceLine(".var/uaconf", 10, input)
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		kernal.replaceLine(".var/uaconf", 12, input)
		kernal.replaceLine(".var/settings", 8, "New User: False")
		fs.delete(".var/.tmp")
		shell.run(".var/gui")

end
		
if kernal.getLine(".var/settings", 3)=="Auto Log-in: True" then
fs.delete(".var/.tmp")
shell.run(".var/gui")
elseif kernal.getLine(".var/settings", 3)=="Auto Log-in: False" then

image = paintutils.loadImage(".var/login.nfp")
paintutils.drawImage(image,1 ,1)
term.setBackgroundColor(1)
term.setCursorPos(19, 11)
term.setTextColor(128)
print("User Name")
term.setCursorPos(18, 12)
term.setBackgroundColor(256)
print("           ")
term.setBackgroundColor(colors.white)
term.setCursorPos(19, 14)
term.setTextColor(128)
print("Password")
term.setCursorPos(18, 15)
term.setBackgroundColor(256)
print("           ")

term.setCursorPos(19, 12)
term.setTextColor(128)

write("")
input = read()
if input == usr1 then
term.setCursorPos(19, 15)
	write("")
	input = read("*")
	if input == pas1 then
		shell.run(".var/gui")
		fs.delete(".var/.tmp")
		else
			os.reboot()
	end
	
	elseif input == usr2 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas2 then
			shell.run(".var/gui")
			fs.delete(".var/.tmp")
			else
				os.reboot()
		end
		
	elseif input == usr3 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas3 then
		fs.delete(".var/.tmp")
			shell.run(".var/gui")
			else
				os.reboot()
		end

	elseif input == usr4 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas4 then
		fs.delete(".var/.tmp")
			shell.run(".var/gui")
			else
				os.reboot()
		end
		
	elseif input == usr5 then
		term.setCursorPos(19, 15)
		write("")
		input = read("*")
		if input == pas5 then
		fs.delete(".var/.tmp")
			shell.run(".var/gui")
			else
				os.reboot()
		end
		
		
		else


cUser = input
term.setCursorPos(19, 15)
usr = read("*")
cloudID = usr
term.setCursorPos(14, 16)
	term.setBackgroundColor(1)
	print("Signing Into the Cloud...")

http.post(
                            "http://ndfos.ndfjay.co.uk/login/checklogin2.php?username="..textutils.urlEncode(tostring(cUser)).."&password="..textutils.urlEncode(tostring(cloudID))
                )

http.request("http://ndfos.ndfjay.co.uk/login/"..cUser)

 local requesting = true

 while requesting do

   local event, url, sourceText = os.pullEvent()

   if event == "http_success" then

     local respondedText = sourceText.readAll()
	 
	term.setCursorPos(13, 16)
	term.setBackgroundColor(1)
	print("Syncing Cloud Storage...")
	term.setCursorPos(13, 18)
	print("   Please Stand By..")
	fs.makeDir("documents/Cloud")
	kernal.replaceLine(".var/settings", 2,"Enable Terminal: True")
	sync = "documents/Cloud/"
		cURL = "http://ndfos.ndfjay.co.uk/register/source/Cloud_data/Members/"
		cLog = cUser.."/"
		files = "files"
		local osEVer = http.get(tostring(cURL..cLog..files))
		kernal.fwrite(".var/.files", osEVer.readAll())
		osEVer.close()
		cFiles = kernal.getTable(".var/.files")
		for i,v in ipairs(cFiles) do
		local osEVer = http.get(tostring(cURL..cLog.."Files/"..v))
		kernal.fwrite(sync..v, osEVer.readAll())
		osEVer.close()
			
		end
	
		kernal.replaceLine(".var/.adsnt.dll", 1, "http://ndfos.ndfjay.co.uk/register/source/Cloud_data/Members/"..cUser.."/Files/sync.php?name=")
		kernal.replaceLine(".var/.adsnt.dll", 2, "http://ndfos.ndfjay.co.uk/register/source/Cloud_data/Members/"..cUser.."/sync.php?name=")
	 	kernal.replaceLine(".var/.adsnt.dll", 3, cUser)
		kernal.replaceLine(".var/.adsnt.dll", 4, cloudID)
	 http.post(
                            "http://ndfos.ndfjay.co.uk/login/logout.php?message="..textutils.urlEncode(tostring(cUser))
                )
				
	 shell.run(".var/gui")

     requesting = false


   elseif event == "http_failure" then
	term.setCursorPos(16, 16)
	term.setBackgroundColor(1)
	term.clearLine()
	print("Incorrect Details...")
	term.setCursorPos(16, 18)
	term.setBackgroundColor(1)
	print(" Please try again.")
	sleep(2)
	os.reboot()
	
     requesting = false

   end
 end
	
end
end


end